package org.lele.controller;

import com.alibaba.fastjson.JSONObject;
import org.lele.utils.PdfUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;

import static org.lele.utils.PdfUtils.*;

/**
 * @Author: fanYuLe
 * @CreateTime: 2024-10-31  15:14
 * @Description:
 */
@RestController
@RequestMapping("/download")
public class DownloadController {

    @PostMapping("/pdf")
    public void downloadPdf(HttpServletResponse response) throws IOException {
        byte[] bytes1 = loadHtmlFileAsByteArray("html/付款审批.html");
        String filePath = "C:\\Users\\13037\\Desktop\\新建文件夹 (2)\\付款审批.html"; // 替换为你的HTML文件路径

        if (bytes1 != null) {
            // 打印字节数组的长度，或者根据需要使用字节数组
            System.out.println("File size in bytes: " + bytes1.length);
            String html = new String(bytes1);
            System.out.println("html = " + html);
            JSONObject jsonObject = PdfUtils.setJsonObjectValue();
            String fileName = "付款审批单";
            MultipartFile file =  PdfUtils.genPdfByStringTemplateAndMakeWater(html, jsonObject, fileName, "");
            ArrayList<MultipartFile> multipartFiles = new ArrayList<>();
            multipartFiles.add(file);
            //单个下载
            //PdfUtils.download(file, response);
            //批量下载
            compressFilesToZip(multipartFiles, "采购申购单下载", response);

        }

        /*byte[] fileBytes = htmlFileToByteArray(filePath);
        if (fileBytes != null) {
            // 打印字节数组的长度，或者根据需要使用字节数组
            System.out.println("File size in bytes: " + fileBytes.length);
            String html = new String(fileBytes);
            System.out.println("html = " + html);
            JSONObject jsonObject = PdfUtils.setJsonObjectValue();
            String fileName = "付款审批单";
            MultipartFile file =  PdfUtils.genPdfByStringTemplateAndMakeWater(html, jsonObject, fileName, "");
            PdfUtils.download(file, response);
        }*/
    }
}
